import { create } from 'zustand'
import { subscribeWithSelector } from 'zustand/middleware'

export default create(subscribeWithSelector((set) => {
  return {


    // 可以玩的时间
    duration: 30,

    updateTimer: (val) => {
      set(state => {
        return {
          duration: state.duration - 1
        }
      })
    },

    passed: 0,

    /**
     * Time
     */
    startTime: 0,
    endTime: 0,

    /**
     * Phases
     */
    phase: 'ready',

    points: 0,
    target: 24,

    increase: (val) => {
      set(state => {
        // console.log(`increase.phase`, state.phase)
        if (state.phase === 'playing') return {
          points:val
        }
        return {}
      })
    },

    change: (key, val) => {
      set(state => {
        return {
          [key]: val
        }
      })
    },

    start: () => {
      set((state) => {
        console.log("state.phase)", state.phase)
        if (state.phase === 'ready')
          return {phase: 'playing', startTime: Date.now()}

        return {}
      })
    },

    restart: () => {
      set((state) => {
        if (state.phase === 'playing' || state.phase === 'ended')
          return {phase: 'ready', blocksSeed: Math.random()}

        return {}
      })
    },

    loss: () => {
      set((state) => {
        if (state.phase === 'playing')
          return {phase: 'loss', endTime: Date.now()}

        return {}
      })
    },

    end: () => {
      set((state) => {
        // console.log("hhhhhhhhhh")
        if (state.phase === 'playing')
          return {phase: 'ended', endTime: Date.now()}

        return {}
      })
    }
  }
}))